package com.log.compone;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

/**
 * @Description TODO
 * @Author zhaoYi
 * @Date 2024/7/17 0:05
 */
public class SelectorClientTest {

    public static void main(String[] args) throws Exception {
        //获取通道
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("localhost", 9999));
        //切换非阻塞模式
        socketChannel.configureBlocking(false);
        // 创建缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //发送数据到服务端
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("请输入：");
            String str = sc.next();
            buffer.put(("小明: "+str).getBytes());
            buffer.flip();
            socketChannel.write(buffer);
            buffer.clear();
            //非界面操作暂无需关闭通道
//            socketChannel.close();
        }
    }
}
